from plone.app.portlets.portlets import calendar
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Acquisition import aq_inner, aq_base, aq_parent
from DateTime import DateTime
from zope.component import getMultiAdapter

class CalendarRenderer(calendar.Renderer):
      #use customized template
      _template = ViewPageTemplateFile('templates/calendar.pt') 
      
      def getEventClass(self, day):
          #import pdb;pdb.set_trace()
          
          res=''
          if day['is_today'] and 'event_special' not in day:
              res='todayevent'
          elif day['is_today'] and 'event_special' in day:
              res='todayevent_special'
          elif 'event_special' in day:
              res='event_special'
          else:
              res='event'
          
          return res
          
      def getEventsForCalendar(self):
        context = aq_inner(self.context)
        year = self.year
        month = self.month
        portal_state = getMultiAdapter((self.context, self.request), name=u'plone_portal_state')
        navigation_root_path = portal_state.navigation_root_path()
        weeks = self.calendar.getEventsForCalendar(month, year, path=navigation_root_path)
        for week in weeks:
            for day in week:
                daynumber = day['day']
                if daynumber == 0:
                    continue
                day['is_today'] = self.isToday(daynumber)
                if day['event']:
                    #import pdb;pdb.set_trace()
                    cur_date = DateTime(year, month, daynumber)
                    localized_date = [self._ts.ulocalized_time(cur_date, context=context, request=self.request)]
                    day['eventstring'] = '\n'.join(localized_date + [self.getEventString(e) for e in day['eventslist']])
                    day['date_string'] = '%s-%s-%s' % (year, month, daynumber)
                    ##To know if we have special event this day
                    res_ = self.calendar.getEventsForThisDay(cur_date)
                    for brain in res_:
                        if brain.getObject().Type()=='EventSpecial':
                            day['event_special']=1
                            break
                    

        return weeks
